﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using YeqianFramework.core;

namespace YeqianFramework.common
{
	public class YQLogCenter : MonoBehaviourSingleton<YQLogCenter>
	{
		private string path = YQFKContext.Current.YQFKConfig.LogDirectory + DateTime.Today.ToString("yyyy-MM-dd") + ".log.md";

		protected override void Init()
		{
			string title = "# Ciallo～(∠ • ω< )╭☆\r\n今天是" + DateTime.Today.ToString("yyyy-MM-dd") + "，今天都发生了什么呢？\r\n\r\n";

			if (!File.Exists(path))
			{
				FileStream fs = File.Create(path);
				byte[] buffer = Encoding.UTF8.GetBytes(title);
				fs.Write(buffer, 0, buffer.Length);
				fs.Close();
				return;
			}
		}


		public void Log(string log, Color color)
		{
#if UNITY_EDITOR
			Debug.Log(string.Format("<color=#{0}>{1}</color>", ColorUtility.ToHtmlStringRGB(color), log));
#else
			WriteLog2File(log);
#endif
		}
		public void Log(object log, Color color)
		{
			Log(log.ToString(), color);
		}

		public void Log(string log)
		{
			Log(log, Color.white);
		}
		public void Log(object log)
		{
			Log(log.ToString());
		}

		public void LogError(string log)
		{
#if UNITY_EDITOR
			Debug.LogError(log);
#else
			WriteError2File(log);
#endif
		}

		/// <summary>
		/// 框架专用
		/// </summary>
		/// <param name="log"></param>
		public void YQFKLog(string log)
		{
#if UNITY_EDITOR
			string ret = string.Format("<color=red>███Ciallo～(∠ • ω< )╭☆███</color>{0}", log);
			Debug.Log(ret);
#else
			string ret = string.Format("**███Ciallo～(∠ • ω< )╭☆███** {0}", log);
			WriteLog2File(log);
#endif
		}

		/// <summary>
		/// 框架专用
		/// </summary>
		/// <param name="log"></param>
		public void YQFKError(string log)
		{
#if UNITY_EDITOR
			string ret = string.Format("<color=red>███(╬▔皿▔)███</color>{0}", log);
			Debug.LogError(ret);
#else
			string ret = string.Format("**███(╬▔皿▔)███** {0}", log);
			WriteError2File(log);
#endif
		}

		private void WriteLog2File(string log)
		{
			string time = DateTime.Now.ToString("hh:mm:ss");
			File.AppendAllText(path, string.Format("#### [{0}]\n {1}\n", time, log));
		}
		private void WriteError2File(string log)
		{
			string time = DateTime.Now.ToString("hh:mm:ss");
			File.AppendAllText(path, string.Format("#### [{0}] <font color=\"red\">[ERROR]</font>\n {1}\n", time, log));
		}
	}
}
